import {messages} from "share/common";
import React from 'react'
import {connect} from 'react-redux'

import menuRoute from "routes/menuRoute";

import { Form, Button ,message } from 'antd'

import ApproveBar from 'components/template/approve-bar'
import requestService from "containers/approve/request/request.service"

class RescheduleRefundBtn extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      passLoading: false,
      rejectLoading: false,
      rescheduleRefundList: menuRoute.getRouteItem('reschedule-refund','key'),
    }
  }

  //返回
  goBack = () => {
    menuRoute.goBack(this)
  };

  //审批通过
  handleApprovePass = (value) => {
    let params = {
      approvalTxt: value,
      entities: [{
        entityOID: this.props.bookTaskOID,
        entityType: 2002, //订票任务
      }]
    };
    this.setState({ passLoading: true });
    requestService.handleRequestApprovePass(params).then(res => {
      if (res.status === 200) {
        message.success(messages('common.operate.success'));
        this.setState({ passLoading: false });
        this.goBack()
      }
    }).catch(e => {
      this.setState({ passLoading: false });
      message.error(`${messages('common.operate.filed')}，${e.response.data.message}`)
    })
  };

  //审批驳回
  handleApproveReject = (value) => {
    let params = {
      approvalTxt: value,
      entities: [{
        entityOID: this.props.bookTaskOID,
        entityType: 2002, //订票任务
      }]
    };
    this.setState({ rejectLoading: true });
    requestService.handleRequestApproveReject(params).then(res => {
      if (res.status === 200) {
        message.success(messages('common.operate.success'));
        this.setState({ rejectLoading: false });
        this.goBack()
      }
    }).catch(e => {
      this.setState({ rejectLoading: false });
      message.error(`${messages('common.operate.filed')}，${e.response.data.message}`)
    })
  };

  render() {
    const { passLoading, rejectLoading, rescheduleRefundList } = this.state;
    return (
      <div className="reschedule-refund-btn">
        {this.props.approving ? (
            <ApproveBar backUrl={rescheduleRefundList.url}
                        passLoading={passLoading}
                        rejectLoading={rejectLoading}
                        handleApprovePass={this.handleApprovePass}
                        handleApproveReject={this.handleApproveReject}
            />
        ) : (
          <Button type="primary" className="back-btn" onClick={this.goBack}>{messages('common.back')}</Button>
        )}
      </div>

    )
  }
}

RescheduleRefundBtn.contextTypes = {
  router: React.PropTypes.object
};

RescheduleRefundBtn.propTypes = {
  approving: React.PropTypes.bool,
  bookTaskOID: React.PropTypes.string
};

function mapStateToProps(state) {
  return {
    user: state.login.user
  }
}

const wrappedRescheduleRefundBtn = Form.create()(RescheduleRefundBtn);

export default connect(mapStateToProps)(wrappedRescheduleRefundBtn)
